package org.classg.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.classg.edu.entity.Comment;
import org.classg.edu.mapper.CommentMapper;
import org.classg.edu.service.CommentService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-12
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    // 查询所有评论分页数据
    @Override
    public Map<String, Object> queryAllCommentPage(String courseId, Page<Comment> page) {
        baseMapper.selectPage(page, new LambdaQueryWrapper<Comment>().orderByDesc(Comment::getGmtCreate).eq(Comment::getCourseId, courseId));
        List<Comment> comments = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();
        // 把分页数据封装到map中
        Map<String, Object> map = new HashMap<>();
        map.put("comments", comments);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }
}
